//
//  HDCFModifyPasswordVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/13.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFModifyPasswordVc.h"
#import "ModifyPasswCell.h"
#import "SVProgressHUD.h"

@interface HDCFModifyPasswordVc ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, copy) NSString *passwordOne;
@property (nonatomic, copy) NSString *passwordTwo;
@property (nonatomic, copy) NSString *passwordThree;
@end

@implementation HDCFModifyPasswordVc

- (void)setIsTradePassword:(BOOL)isTradePassword{
    _isTradePassword = isTradePassword;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        self.titleArray = [NSArray arrayWithObjects:@"原密码", @"新密码", @"确认新密码(6~20个字符，包含数字、字母、符号两种以上)", nil];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 230) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = NO;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 20)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(43, CGRectGetMaxY(tableView.frame), screenWidth - 53, 30);
    label.numberOfLines = 0;
    label.text = @"密码：6~20个字符，包含数字、字母、符号两种以上";
    label.textColor = ZKcolor(189, 44, 50);
    label.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    if (!(_isTradePassword == YES)) {
        label.hidden = YES;
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(40, CGRectGetMaxY(label.frame) + 30, screenWidth - 80, 38);
    button.layer.cornerRadius = 6.0;
    [button setBackgroundColor:ZKcolor(189, 44, 50)];
    [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [button setTitle:@"确认修改" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(confirmPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellID";
    ModifyPasswCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [ModifyPasswCell ModifyPasswCell];
    }
    if (indexPath.row == 0) {
        cell.textField.placeholder = @"原密码";
    }else if (indexPath.row == 1){
        cell.textField.placeholder = @"新密码";
        cell.textField.secureTextEntry = YES;
    }else{
        cell.textField.placeholder = @"确认新密码";
        cell.textField.secureTextEntry = YES;
    }
    [cell.textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    cell.textField.delegate = self;
    cell.textField.tag = indexPath.row + 10;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)textChange:(UITextField *)textField{
    if (textField.tag == 10) {
        self.passwordOne = textField.text;
    }else if (textField.tag == 11){
        self.passwordTwo = textField.text;
    }else{
        self.passwordThree = textField.text;
    }
}

- (void)confirmPassword{
    if (self.passwordOne.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入原来密码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (self.passwordTwo.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (self.passwordThree.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请再次输入密码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (![self.passwordTwo isEqualToString:self.passwordThree]) {
        [SVProgressHUD showInfoWithStatus:@"两次密码不一致"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
    if (_isTradePassword == YES) {
        [self modifyTradePassword];
    }else{
        [self modifyLoginPassword];
    }
    
}

- (void)modifyTradePassword{
    [SVProgressHUD show];
    NSMutableDictionary *dictOne = [NSMutableDictionary dictionary];
    dictOne[@"accountNumber"] = kAcount;
    dictOne[@"prePassword"] = self.passwordOne;
    dictOne[@"newPassword"] = self.passwordTwo;
    
    NSString *jsonString = [self dictionaryToJson:dictOne];
    NSDictionary *paramet = @{
                              KparametTwo:@"/public/investor/investor-update-password",
                              KparametOne:jsonString
                              };
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:nil success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            [SVProgressHUD  dismissWithDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD  dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD  dismissWithDelay:1.0];
    }];
}

 - (void)modifyLoginPassword{
     NSMutableDictionary *dictOne = [NSMutableDictionary dictionary];
     dictOne[@"OldPwd"] = self.passwordOne;
     dictOne[@"NewPwd"] = self.passwordTwo;
     NSString *jsonString = [self dictionaryToJson:dictOne];
     [HttpRequest sendSelfPostRequestWithURL:@"api/User/ModifyPwd" WithParamets:jsonString WithParamet:nil success:^(id result) {
         if ([result[@"code"] isEqual:@1]) {
             [SVProgressHUD showSuccessWithStatus:@"修改成功"];
             [SVProgressHUD  dismissWithDelay:1.0];
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [self.navigationController popViewControllerAnimated:YES];
             });
         }else{
             [SVProgressHUD showInfoWithStatus:result[@"msg"]];
             [SVProgressHUD  dismissWithDelay:1.0];
         }
     } failure:^(NSError *errer) {
         [SVProgressHUD showErrorWithStatus:@"网络错误"];
         [SVProgressHUD  dismissWithDelay:1.0];
     }];
 }

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    //    NSString *jsonTemp2 = [jsonTemp stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    return jsonTemp;
}

@end
